package com.shushan.setting;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Spinner;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.login.bean.ChildBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomDialog;
import com.shushan.util.SharedPreferencesUtils;

/**
 * @ClassName: ChildSettingActivity
 * @Desciption: 学生设置
 * @author: yichaohua
 * @date: 2018-07-13
 */
public class ChildSettingActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener {

    private static final String TAG = "ChildSettingActivity";


    private ImageView backImg;
    private RadioGroup sexRadioGroup,gradeRadioGroup;
    private Button confirmButton;
    private EditText nameEdit;
    private Spinner gradeSpinner;

    private int gradeId = 1;
    private int firstTime = 0;
    private String semester = "UP";
    private int sex = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.child_setting_activity);

        firstTime = getIntent().getIntExtra("firstTime",0);

        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true,this);

        initView();
    }

    private void initView() {
        backImg = findViewById(R.id.child_setting_back_img);
        backImg.setOnClickListener(this);
        sexRadioGroup = findViewById(R.id.child_setting_sex_rg);
        sexRadioGroup.setOnCheckedChangeListener(this);
        gradeRadioGroup = findViewById(R.id.child_setting_grade_rg);
        gradeRadioGroup.setOnCheckedChangeListener(this);
        confirmButton = findViewById(R.id.child_setting_confirm_btn);
        confirmButton.setOnClickListener(this);
        nameEdit = findViewById(R.id.child_setting_name_edit);
        gradeSpinner = findViewById(R.id.child_setting_spinner);
        gradeSpinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.child_setting_back_img:
                showBackDialog();
                break;
            case R.id.child_setting_confirm_btn:
                if(valide()){
                    requestAdd();
                }
                break;
            default:
                break;
        }
    }

    private void showBackDialog() {
        new CustomDialog.Builder(this)
                .setTitle("温馨提示")
                .setMessage("发现您尚未添加小孩，退出将导致无法正常使用app，是否退出设置？")
                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        onBackAction();

                    }
                })
                .setPositiveButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                })
                .create().show();
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.child_setting_boy_rb:
                sex = 1;
                break;
            case R.id.child_setting_girl_rb:
                sex = 2;
                break;
            case R.id.child_setting_up_rb:
                semester = "UP";
                break;
            case R.id.child_setting_down_rb:
                semester = "DOWN";
                break;
            default:
                break;
        }
    }

    private boolean valide() {
        String name = nameEdit.getText().toString();
        if(TextUtils.isEmpty(name)){
            showToast("请输入孩子姓名");
            return false;
        }

        return true;
    }

    private void requestAdd() {
        showWaitDialog();

        int useId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.CHILD_ADD);
        urlParse.putValue("user_id", useId)
                .putValue("child_name", nameEdit.getText().toString())
                .putValue("grade_id", gradeId)
                .putValue("semester", semester)
                .putValue("sex", sex);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<ChildBean>() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(ChildBean childBean) {

                        SharedPreferencesUtils.setIntDate(Constants.CURRENT_CHILD_ID_SHAREPREFERS_KEY, childBean.id);
                        BabyApplication.getInstance().setCurrentChildBean(childBean);
                        BabyApplication.getInstance().chilidAdd(childBean);

                        Intent intent = new Intent(ChildSettingActivity.this,BookSelectActivity.class);
                        intent.putExtra("gradeId",gradeId);
                        intent.putExtra("semester",semester);
                        intent.putExtra("firstTime",firstTime);
                        startActivityAllow(intent);
                        onBackAction();
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"onItemSelected position=" + position);

        gradeId = position + 1;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onBackPressed() {
        showBackDialog();
    }
}
